﻿using System;
using System.Collections.Generic;
using System.Linq;
using Pap.Data;
using Pap.Data.Abstraction.Utils;
using Pap.Web.Utils;

namespace Pap.Web.Modules.Shared.ViewModel
{
    /// <summary>
    /// 
    /// </summary>
    public class InsertAttachmentsViewModel : IViewModel
    {
        #region [ Fields ]
        /// <summary>
        /// 
        /// </summary>
        private IImageRepository _ImageRepository;
        #endregion

        #region [ Constructor ]
        /// <summary>
        /// Initializes a new instance of the <see cref="InsertAttachmentsViewModel"/> class.
        /// </summary>
        /// <param name="imageRepository">The image repository.</param>
        public InsertAttachmentsViewModel(IImageRepository imageRepository)
        {
            this._ImageRepository = imageRepository;
        }
        #endregion

        //public IImage CreateImage(byte[] bytes, Guid typeID, string labels)
        //{
        //    using (var unitOfWork = this.GetInstance<IUnitOfWork>())
        //    {
        //        var image = this.GetInstance<IImage>();
        //        image.ImageID = Guid.NewGuid();
        //        image.ImageData = bytes;
        //        image.ImageTypeID = typeID;
        //        image.Label = labels;
        //        this._ImageRepository.InsertImage(image);
        //        unitOfWork.Commit();
        //        return image;
        //    }
        //}

        /// <summary>
        /// Gets the image types.
        /// </summary>
        /// <returns></returns>
        public IEnumerable<ILookUpEntity> GetImageTypes()
        {
            return this._ImageRepository.GetImageTypes();
        }
        /// <summary>
        /// Deletes the images.
        /// </summary>
        /// <param name="imageIDs">The image I ds.</param>
        public void DeleteImages(IEnumerable<Guid> imageIDs)
        {
            using (var unitOfWork = this.GetInstance<IUnitOfWork>())
            {
                bool imagesExisit = _ImageRepository.FindImage(image => imageIDs.Contains(image.ImageID)).Count() > 0;
                if (imagesExisit)
                {
                    foreach (var imageID in imageIDs)
                    {
                        this._ImageRepository.DeleteImage(imageID);
                    }
                }
                unitOfWork.Commit();
            }
        }
        /// <summary>
        /// Does the save.
        /// </summary>
        /// <param name="images">The images.</param>
        /// <param name="deletedImages">The deleted images.</param>
        public void DoSave(IList<IImage> images, IList<Guid> deletedImages)
        {
            var insertedImages = images.OfType<InMemoryImage>();
            var updatedImages = images.Where(i => !(i is InMemoryImage));
            //---------------------------------------------------------------------------------------------------------------------------
            var imagesNeedToBeUpdated = _ImageRepository.FindImage(image => updatedImages.Select(i => i.ImageID).Contains(image.ImageID));
            foreach (IImage imageToUpdate in imagesNeedToBeUpdated)
            {
                var updatedImage = updatedImages.Single(i => i.ImageID == imageToUpdate.ImageID);
                imageToUpdate.ImageTypeID = updatedImage.ImageTypeID;
                imageToUpdate.Label = updatedImage.Label;
                //imageToUpdate.ImageData = updatedImage.ImageData;
            }
            //----------------------------------------------------------------------------------------------------------------------------
            _ImageRepository.DeleteImages(_ImageRepository.FindImage(i => true).Where(image => deletedImages.Contains(image.ImageID)));
            //-----------------------------------------------------------------------------------------------------------------------------
            foreach (InMemoryImage image in insertedImages)
                _ImageRepository.InsertImage((Image)image);
            //----------------------------------------------------------------------------------------------------------------------------
        }
    }
}
